package com.example.armmvvm.ui.customdraw.view

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import com.example.arm.ext.dp

/**
 * 进度条
 *
 * @author 创建人 ：yanghaozhang
 * @version 1.0
 * @package 包名 ：com.example.armmvvm.ui.main
 * @createTime 创建时间 ：2021/9/9
 * @modifyBy 修改人 ：
 * @modifyTime 修改时间 ：
 * @modifyMemo 修改备注：
 */
class ComplexProgressView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    data class ProgressItem(val value: Int, val color: Int)

    // 背景
    lateinit var bgPaint: Paint

    // 进度
    lateinit var progressPaint: Paint

    // 起始/终止半圆
    lateinit var halfCirclePaint: Paint

    // 分割线
    lateinit var dividerPaint: Paint

    var halfHeight = 0f

    var halfWidth = 0f

    private var bgColor = Color.parseColor("#FFC4DFF9")

    private var dividerColor = Color.WHITE

    private val progressItemList = mutableListOf<ProgressItem>()

    private var progressDenominator = 0

    init {
        progressItemList.add(ProgressItem(3, Color.YELLOW))
        progressItemList.add(ProgressItem(3, Color.BLUE))
        progressItemList.add(ProgressItem(2, Color.BLACK))
        progressItemList.add(ProgressItem(2, Color.GREEN))
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        halfHeight = height.toFloat() / 2
        halfWidth = width.toFloat() / 2
        initBgPaint()
        canvas.drawLine(
            0f + halfHeight,
            halfHeight,
            width.toFloat() - halfHeight,
            halfHeight,
            bgPaint
        )

        initProgressPaint()
        initHalfCirclePaint()
        initDividerPaint()

        val dividerList = mutableListOf<Double>()
        var drawStartX = halfHeight.toDouble()
        var percentSum = 0
        var isFirst = true
        for ((index, value) in progressItemList.withIndex()) {
            if (value.value == 0) {
                continue
            }
            percentSum += value.value
            progressPaint.color = value.color
            drawStartX = drawProgress(
                canvas,
                value.value.toDouble() / progressDenominator,
                drawStartX.toFloat(),
                progressPaint,
                isFirst,
                index == progressItemList.size - 1 && percentSum == progressDenominator
            )
            dividerList.add(drawStartX)
            isFirst = false
        }
        for ((index, value) in dividerList.withIndex()) {
            if (!(index == dividerList.size - 1 && percentSum == progressDenominator)) {
                drawDivider(canvas, value.toFloat(), dividerPaint)
            }
        }
        if (percentSum == progressDenominator && progressItemList.isNotEmpty()) {
            drawEndHalfCircle(canvas, progressItemList.last().color)
        }
    }

    private fun initDividerPaint() {
        if (!this::dividerPaint.isInitialized) {
            dividerPaint = Paint().apply {
                style = Paint.Style.FILL
                isAntiAlias = true
                strokeWidth = 4.dp
                color = dividerColor
            }
        }
    }

    private fun initHalfCirclePaint() {
        if (!this::halfCirclePaint.isInitialized) {
            halfCirclePaint = Paint().apply {
                style = Paint.Style.FILL_AND_STROKE
                isAntiAlias = true
                strokeWidth = 1f
            }
        }
    }

    private fun initProgressPaint() {
        if (!this::progressPaint.isInitialized) {
            progressPaint = Paint().apply {
                style = Paint.Style.STROKE
                isAntiAlias = true
                strokeWidth = height.toFloat()
            }
        }
    }

    private fun initBgPaint() {
        if (!this::bgPaint.isInitialized) {
            bgPaint = Paint().apply {
                style = Paint.Style.STROKE
                isAntiAlias = true
                strokeWidth = height.toFloat()
                strokeCap = Paint.Cap.ROUND
                color = bgColor
            }
        }
    }

    private fun drawEndHalfCircle(canvas: Canvas, color: Int) {
        val arc = RectF(width.toFloat() - height, 0f, width.toFloat(), height.toFloat())
        halfCirclePaint.color = color
        canvas.drawArc(arc, 90f, -180f, true, halfCirclePaint)
    }

    private fun drawProgress(
        canvas: Canvas,
        percent: Double,
        startX: Float,
        paint: Paint,
        isStart: Boolean = false,
        isEnd: Boolean = false
    ): Double {
        val trueStartX = if (isStart) halfHeight else startX
        if (isStart) {
            val arc = RectF(0f, 0f, height.toFloat(), height.toFloat())
            halfCirclePaint.color = paint.color
            canvas.drawArc(arc, 270f, -180f, true, halfCirclePaint)
        }
        val endX = if (isEnd) (width - halfHeight).toDouble() else trueStartX + (width - height) * percent
        canvas.drawLine(startX, halfHeight, endX.toFloat(), halfHeight, paint)
        return endX
    }

    private fun drawDivider(canvas: Canvas, xPoint: Float, paint: Paint) {
        val path = Path().apply {
            moveTo(xPoint, 0f)
            lineTo(xPoint + paint.strokeWidth / 2, 0f)
            lineTo(xPoint, height.toFloat())
            lineTo(xPoint - paint.strokeWidth / 2, height.toFloat())
        }
        canvas.drawPath(path, paint)
    }

    //-------------------------外部接口-------------------------

    fun setBgColor(color: Int) {
        bgColor = color
        initBgPaint()
        bgPaint.color = color
    }

    fun setDividerColor(color: Int) {
        dividerColor = color
        initDividerPaint()
        dividerPaint.color = color
    }

    fun setProgressList(list: List<ProgressItem>) {
        progressItemList.clear()
        progressItemList.addAll(list)
    }

    fun setProgressDenominator(value: Int) {
        progressDenominator = value
    }
}